package cn.com.artemis.biz.steinsggs.web.intercepter;

import cn.com.artemis.biz.steinsggs.common.util.RedisCache;
import cn.com.artemis.biz.steinsggs.web.Constants;
import cn.com.artemis.biz.steinsggs.web.RedisKey;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * LoginStatusInterceptor
 * 登陆状态拦截器
 *
 * @author
 * @version V1.0.0
 * @since
 */
public class LoginStatusInterceptor extends HandlerInterceptorAdapter implements Constants, RedisKey {


    private static final Logger logger = LoggerFactory.getLogger(LoginStatusInterceptor.class);

    @Resource
    private RedisCache redisCache;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String contextPath = (String) request.getAttribute("contextPath");

        String uri = request.getRequestURI();
        if(isIgnore(uri)){
            return true;
        }
        //监控都有权限
        if(uri.contains(contextPath +"/monitor.html")||uri.contains(contextPath +"/f5monitor.html")){
            return true;
        }

        return true;
    }


    /**
     * isIgnore
     * 忽略的url
     *
     * @param path
     * @return boolean
     *
     * @version V1.0.0
     * @author <a href="chufeng.xu@downjoy.com">徐楚风</a>
     * @since 2017-02-16 15:21
     */
    public static boolean isIgnore(String path) {
        if (path != null && (
                path.contains("/css/") ||
                        path.contains("/img/") ||
                        path.contains("/resources/") ||
                        path.contains("/js/") ||
                        path.contains("/themes/") ||
                        path.contains("/login")
        )
                ) {
            return true;
        }
        return false;
    }

    /**
     * setResponse
     *  设置返回的json或者登陆页
     *
     * @param request
     * @param response
     * @return void
     *
     * @version V1.0.0
     * @author <a href="chufeng.xu@downjoy.com">徐楚风</a>
     * @since 2017-02-16 15:21
     */
    public static void setResponse(HttpServletRequest request, HttpServletResponse response){
        if("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With")) || request.getParameter("ajax") != null) {
            response.setContentType("application/json; charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out=null;
            try {
                out=response.getWriter();
                Map<String, Object> result = new HashMap<String, Object>();
                result.put("statusCode", "301");
                result.put("message", "会话超时，请重新登录!");
                result.put("href", request.getAttribute("basePath")+"/login/index.html");

                out.println(JSON.toJSONString(result,false));
                out.flush();
            } catch(IOException e) {
            } finally {
                if(out != null) {
                    out.close();
                    out=null;
                }
            }
        } else {
            try {
                response.sendRedirect(request.getAttribute("basePath")+"/login/index.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
